<template>
  <div class='publish-container'>
    <el-card>
      <div class="header" style="padding-bottom:15px">
        <my-breadcrumb>编辑文章</my-breadcrumb>
      </div>
      <el-form ref="form" :rules="rules" :model="article" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model.trim="article.title"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <quill-editor v-model="article.content" :options="editorOption"></quill-editor>
          <!-- <el-input type="textarea" v-model="article.content"></el-input> -->
        </el-form-item>
        <el-form-item label="封面">
          <!-- 切换封面类型时重置封面数组 -->
          <el-radio-group v-model="article.cover.type" @change="article.cover.images=[]">
            <el-radio :label="1">单图</el-radio>
            <el-radio :label="3">三图</el-radio>
            <el-radio :label="0">无图</el-radio>
            <el-radio :label="-1">自动</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-if="article.cover.type > 0">
          <el-row :gutter="10">
            <el-col
            v-for="(item, idx) in article.cover.type" :key="idx"
             class="img_item" :xs="8" :sm="6" :md="6" :lg="4">
              {{idx}}<my-cover v-model="article.cover.images[idx]"></my-cover>
            </el-col>
          </el-row>
        </el-form-item>
        <my-channels v-model="article.channel_id"></my-channels>
        <el-form-item>
          <el-button type="primary" @click="hSaveArticle">保存</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>
<script>
import MyChannels from '@/components/MyChannelsVmodel'
import MyBreadcrumb from '../../components/MyBreadcrumb.vue'
import { modArticle, getArticle } from '@/api/article'
import MyCover from '../../components/MyCover.vue'

// 导入样式
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
// 导入组件
import { quillEditor } from 'vue-quill-editor'
// import { getArticles } from '../../api/article'
export default {
  name: 'EditArticle',
  components: {
    MyBreadcrumb,
    quillEditor,
    MyChannels,
    MyCover
  },
  props: {},
  data () {
    return {
      rules: {
        title: [
          { required: true, message: '请输入文章标题', trigger: 'blur' },
          { min: 5, max: 30, message: '长度在5到30之间', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '请输入文章内容', trigger: 'blur' },
          { min: 20, max: 30000, message: '最少20个字', trigger: 'blur' }
        ],
        channel_id: [
          { required: true, message: '请选择文章频道', trigger: 'blur' }
        ]
      },
      article: {
        title: 'this-is-标题', // 文章标题
        content: '我读书少，你不要骗我', // 文章内容
        cover: { // 封面
          type: 0, // 封面图片的张数
          images: [] // 封面的地址
        },
        channel_id: '' // 频道id
      },
      // channels: [], // 保存获取频道
      editorOption: {
        // 占位配置
        placeholder: '',
        modules: {
          // 配置工具栏
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'],
            ['blockquote', 'code-block'],
            [{ header: 1 }, { header: 2 }],
            [{ list: 'ordered' }, { list: 'bullet' }],
            [{ indent: '-1' }, { indent: '+1' }],
            ['image']
          ]
        }
      }
    }
  },
  computed: {},
  created () {
    // this.loadChannels()
    // 传入当前要编辑 的文章的id
    this.loadArticle(this.$route.params.id)
  },
  methods: {
    async loadArticle (id) {
      const result = await getArticle(id)
      console.log(result)
      // 保存数据
      this.article = result.data.data
    },
    // async loadChannels () {
    //   try {
    //     const res = await getArticleChannels()
    //     this.channels = res.data.data.channels
    //   } catch (err) {
    //     console.log(err)
    //   }
    // },
    hSaveArticle (isDraft) {
      // 先要做验证
      // this.$refs.form 是用来获取页面有ref="form"那个组件
      // validate()是el-form组件提供的验证方法
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          // alert('submit!')
          try {
            const result = await modArticle(this.article.id, this.article)
            console.log(result)
            this.$message.success('修改成功')
            // 清空发布区域
            // this.article.title = ''
            // this.article.content = ''
          } catch (err) {
            console.log(err)
            this.$message.error('修改失败')
          }
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
// 在父组件中去设置子组件的样式，加上/deep/ 实现穿透功能
.publish-container /deep/ .ql-editor{
  height: 300px;
}
.publish-container /deep/ .ql-toolbar.ql-snow{
  padding: 0 8px;
}
</style>
